package com.sysu.ooad.action;

//import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.Action;
import com.sysu.ooad.dto.GroupFacade;
import com.sysu.ooad.entity.Group;
import com.sysu.ooad.entity.Task;
import com.sysu.ooad.entity.Teacher;
import com.sysu.ooad.service.TaskService;
import com.sysu.ooad.util.AjaxCommand;
import com.sysu.ooad.util.GroupFacadeListJsonConfigFactory;
import com.sysu.ooad.util.JsonConfigFactory;

@Component
@Scope("prototype")
public class TaskTeacherAction extends CommonAction{

	private String title;
	private String requirement;
	private Date deadline;
	private TaskService taskService;
	private List<Task> taskList;
	private Task task;
	private List<GroupFacade> groupList;
	private Group group;
	private int maxScore;
	private int minScore;
	
	public int getMaxScore() {
		return maxScore;
	}
	public void setMaxScore(int maxScore) {
		this.maxScore = maxScore;
	}
	public int getMinScore() {
		return minScore;
	}
	public void setMinScore(int minScore) {
		this.minScore = minScore;
	}
	public Group getGroup() {
		return group;
	}
	public void setGroup(Group group) {
		this.group = group;
	}
	public Task getTask() {
		return task;
	}
	public void setTask(Task task) {
		this.task = task;
	}
	public List<GroupFacade> getGroupList() {
		return groupList;
	}
	public void setGroupList(List<GroupFacade> groupList) {
		this.groupList = groupList;
	}
	public List<Task> getTaskList() {
		return taskList;
	}
	public void setTaskList(List<Task> taskList) {
		this.taskList = taskList;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getRequirement() {
		return requirement;
	}
	public void setRequirement(String requirement) {
		this.requirement = requirement;
	}
	public Date getDeadline() {
		return deadline;
	}
	public void setDeadline(Date deadline) {
		this.deadline = deadline;
	}
	public TaskService getTaskService() {
		return taskService;
	}
	@Resource
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	@Override
	public String execute() throws Exception {
		Teacher teacher = this.findCurrentTeacher();
		taskList = taskService.findTasksByCourse(teacher.getCourse());
		return Action.SUCCESS;
	}
	
	public String addTask() throws Exception {
		System.out.println(title);
		Teacher teacher = (Teacher)session.get("teacher");
		Task task = taskService.addTask(title, requirement, deadline, teacher.getCourse());
		return Action.SUCCESS;
	}

	public String showHomeworks() throws Exception {
		Teacher teacher = this.findCurrentTeacher();
		List<GroupFacade> groups = taskService.findGroupsFacadeWithHomeworks(teacher.getCourse(), task);
		HttpServletResponse response = ServletActionContext.getResponse();
		JsonConfigFactory jsonConfigFactory = new GroupFacadeListJsonConfigFactory();
		AjaxCommand.ajaxReturnJSONArray(response, groups, jsonConfigFactory.createJsonConfig());
		return null;
	}
	
	public String giveScore() {
		taskService.giveScoreToGroup(task, group, maxScore, minScore);
		return null;
	}
	
	public String updateTask() throws Exception {
		taskService.updateTask(task);
		return null;
	}
}
